package com.itshops.controller;

import com.itshops.entity.Address;
import com.itshops.entity.User;
import com.itshops.service.AddressService;
import com.itshops.service.impl.AddressServiceImpl;
import com.itshops.utils.Constants;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/address")
public class AddressController extends BaseServlet{
    AddressService addressService=new AddressServiceImpl();
    public String show(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        //判断登录
        User user = (User) req.getSession().getAttribute("loginUser");
        if (user == null) {
            req.setAttribute("msg","请登录!");
            return Constants.FORWARD+"login.jsp";
        }
        int uid = user.getUid();
        List<Address> address =addressService.findAll(uid);
        //响应
        req.setAttribute("list",address);
        return Constants.FORWARD+"/self_info.jsp";
    }

    //添加地址
    public String add(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        //获取请求参数
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String detail = req.getParameter("detail");
        String auid = req.getParameter("auid");
        Address address = new Address();
        address.setAname(name);
        address.setAdetail(detail);
        address.setAphone(phone);
        address.setUid(Integer.parseInt(auid));
        //调用添加地址业务
        addressService.addAddress(address);
        //响应
        return Constants.FORWARD+"/address?method=show";
    }

    //删除地址
    public String delete(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        //获取请求参数
        String aid = req.getParameter("aid");

        //调用添加地址业务
        addressService.deleteAddress(aid);
        //响应
        return Constants.FORWARD+"/address?method=show";
    }
    //设置默认地址
    public String setDefault(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        //获取请求参数
        String aid = req.getParameter("aid");
        User user = (User) req.getSession().getAttribute("loginUser");
        if (user == null) {
            req.setAttribute("msg","请登录!");
            return Constants.FORWARD+"login.jsp";
        }
        int uid = user.getUid();
        //调用添加地址业务
        addressService.setAddressDefault(aid,uid);
        //响应
        return Constants.FORWARD+"/address?method=show";
    }

    //修改地址数据
    public String update(HttpServletRequest req, HttpServletResponse resp) throws SQLException, InvocationTargetException, IllegalAccessException {
        //获取请求参数
        Map<String, String[]> map = req.getParameterMap();
        Address address = new Address();
        BeanUtils.populate(address,map);
        //调用添加地址业务
        addressService.updateAddress(address);
        //响应
        return Constants.FORWARD+"/address?method=show";
    }

}
